// pages/SCM/store/warehouseItem/warehouseItem.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    warehouseGoods: [],
    categories: [],
    hideFlag:true,
    inputName: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
          let that=this;
          that.setData({
            scrollHeight: app.getScrollHeight(105)
          })
      if(options.id){
        this.data.warehouseId=options.id
              lbbService.warehouseItem({id:options.id}, function (res) {
                      that.data.warehouseItem=res.data;
                wx.setNavigationBarTitle({
                        title: res.data.name
                })
        })
      }
  },
  inputName: function (e) {
    let inputName = e.detail.value
    this.setData({
        inputName: inputName
    })
},
searchGoods: function () {
  let that = this;
  if(that.data.inputName!=""){
    var warehouseGoodsed  = that.data.wanglingList.filter((item)=>{
      return item.goods.name.indexOf(that.data.inputName) != -1
    })
    that.setData({
      warehouseGoods: warehouseGoodsed
    })
  }else{
    that.getWarehouseGoods();
  }

},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    that.toast = that.selectComponent("#toast")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getGoodsCategoryList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toggleTabed(e){
    this.setData({ tabNum: '' });
    // 获取全部数据
    this.getGoodsCategoryList();
  },
  toggleTab: function (e) {
    let id = e.currentTarget.dataset.id;
    if (id == this.data.tabNum){
           id=''; 
    }
    this.setData({ tabNum: id });
    this.getWarehouseGoods();
  },
        toRunningWater: function (e) {
                let item = e.currentTarget.dataset.item;
                let params = '?tabNum=1&goodsStatId=' + item.goodsId;
                params += ('&goodsName=' + item.goods.name)
                params += ('&whid=' + this.data.warehouseId)
                wx.navigateTo({
                        url: '../entryOutLog/entryOutLog' + params,
                })
        },
  getWarehouseGoods: function () {
    let that = this
    let params = {};
    let tabNum = that.data.tabNum;
    if (tabNum) {
      params.categoryId = tabNum;
    }
    if (that.data.warehouseId) {
      params.warehouseId = that.data.warehouseId;
    }
    lbbService.getWarehouseGoods(params, function (res) {
      that.setData({
        warehouseGoods: res.data,
        wanglingList: res.data
      })
    })
  },
  getGoodsCategoryList: function () {
    let that = this;
    lbbService.getGoodsCategoryList({}, function (res) {
      //let tabNum = res.data[0].id;
            let tabNum = '';
      that.setData({
        categories: res.data,
        tabNum: tabNum,
      });
        that.getWarehouseGoods();
    })
  },
  toGoodsAccess:function(e){
        let that=this;
        console.log('toDetail>>', e);
        let optaction=e.currentTarget.dataset.optaction;
        let param = '?optaction=' + optaction + '&warehouseId=' + that.data.warehouseId+'&warehouseName='+that.data.warehouseItem.name;
        wx.navigateTo({
                url: '../goodsAccess/goodsAccess'+param,
        })
        //that.setData({optaction:optaction})   
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    let data = app.touch._touchstart(e, this.data.warehouseGoods)
    this.setData({
      warehouseGoods: data
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    let data = app.touch._touchmove(e, this.data.warehouseGoods)
    this.setData({
      warehouseGoods: data
    })
  },

  //盘点事件
  del: function (e) {
    let that=this;
    let item = e.currentTarget.dataset.item;
    this.setData({
      hideFlag: false,
      cheeckGoods:item
     });
  },
  
  hideModal: function () {
    this.setData({ hideFlag: true });
  },
  stopMaoPao: function () {
    return true;
  },
  sufNumInp: function (e) {
    let sufNum = e.detail.value;
    this.setData({
      sufNum: sufNum
    })
  },
  checkSubmit: function (e) {
    let that = this;
    if (that.data.sufNum !== '0' ) {
      var r = /^\+?[1-9][0-9]*$/;
      if (!r.test(that.data.sufNum)) {
        that.toast.showToast('请输入大于0的整数');
        return false;
      }
    }
    let formData=e.detail.value;
    lbbService.goodsCheckSingleSave(formData, function (res) {
      if (res.result == 0) {
        that.toast.showToast('盘点成功', 'success');
        that.getWarehouseGoods();
        that.hideModal();
        
      } 

    })
  },

})